package com.zch.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;

import com.base.util.DateUtil;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import com.item.service.MessageService;

import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;
import com.zch.dao.model.Activitymessage;
import com.zch.dao.model.ZaitiType;
import com.zch.dao.model.ZxMessage;
import com.zch.dao.model.ZxMessageExample;
import com.zch.daoEx.model.XqmessageEx;
import com.zch.daoEx.model.ZxMessageEx;
import com.zch.service.ZxMessageService;

@RequestMapping("zxMessage")
@Controller
@LoginFilter
public class ZxMessageController extends CoreController{

	@Autowired
	private ZxMessageService zxMessageService;
	@Autowired
    private MessageService messageService;
	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows, String name){
        PaginationSupport.byPage(page, rows);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", name);
        List<ZxMessageEx> list = zxMessageService.selectList(map);     

        return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(ZxMessage zxMessage){
		if (StringUtils.isBlank(zxMessage.getId())){
			zxMessageService.insert(zxMessage);
		}else{
			zxMessageService.updateByPrimaryKeySelective(zxMessage);
			zxMessage = zxMessageService.selectByPrimaryKey(zxMessage.getId());
            String pushContent = "";
            if (zxMessage.getIsAgree() == 2){
                pushContent = "审核未通过！原因:"+zxMessage.getNotAgreeReason();
                messageService.findMessage(zxMessage.getZxTitle(), pushContent, zxMessage.getUserId(), 1);
            }
			
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		ZxMessage zxMessage = zxMessageService.selectByPrimaryKey(id);
		zxMessage.setContent(zxMessage.getContent().replaceAll("\r\n", "<br/>"));
		return JSONUtils.serialize(zxMessage);
	}
	
	@RequestMapping("/findByIds")
    @ResponseBody
    public String finds(String id){
        ZxMessage zxMessage = zxMessageService.selectByPrimaryKey(id);
        zxMessage.setContent(zxMessage.getContent().replaceAll("\r\n", "<br/>").replaceAll("\r", "<br/>").replaceAll("\n", "<br/>").replaceAll("\\s", "&nbsp;"));
        return JSONUtils.serialize(zxMessage);
    }

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			zxMessageService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}
    @RequestMapping("/update")
    @ResponseBody
    public String update(ZxMessage zxMessage){
        int result = zxMessageService.updateByPrimaryKeySelective(zxMessage);
        if(result == 1){
            zxMessage = zxMessageService.selectByPrimaryKey(zxMessage.getId());
            String pushContent = "";
            if(zxMessage.getIsAgree() == 1){
                 pushContent = "审核通过！";
                 messageService.findMessage(zxMessage.getZxTitle(), pushContent, zxMessage.getUserId(), 1);
            }
        }
        
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/mylist")
    @ResponseBody
    public String mylist(Integer page, Integer rows, String name){
        PaginationSupport.byPage(page, rows);
        String id = getSessionParameter("id");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("name", name);
        List<ZxMessageEx> list = zxMessageService.selectMyList(map);

        return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
    }
    
    @RequestMapping("/myListSave")
    @ResponseBody
    public String myListSave(ZxMessage zxMessage){
        zxMessage.setUserId(getSessionParameter("id"));
        if(!getSessionParameter("sys").equals("inside")){
            return JSONUtils.serialize(new Ret(-1,"请重新登录..."));
        }
        if (StringUtils.isBlank(zxMessage.getId())){
            zxMessageService.insert(zxMessage);
        }else{
            zxMessageService.updateByPrimaryKeySelective(zxMessage);
        }
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/top")
    @ResponseBody
    public String top(ZxMessage zxMessage){
        if (StringUtils.isBlank(zxMessage.getId())){
            zxMessage.setPublishTime(new Date());
            zxMessageService.insert(zxMessage);
        }else{
            zxMessage.setPublishTime(new Date());
            zxMessageService.updateByPrimaryKeySelective(zxMessage);
        }
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/cancelTops")
    @ResponseBody
    public String cancelTops(ZxMessage zxMessage){
        if (StringUtils.isBlank(zxMessage.getId())){
            zxMessage.setPublishTime(null);
            zxMessageService.insert(zxMessage);
        }else{
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", zxMessage.getId());
            int result = zxMessageService.cancleTops(map);
        }
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/becomeBanner")
    @ResponseBody
    public String becomeBanner(ZxMessage zxMessage){
        ZxMessageExample example = new ZxMessageExample();
        example.createCriteria().andIsIndexImageEqualTo(1);
        List<ZxMessage> count = zxMessageService.selectByExample(example);
        if(count.size()<5){
            zxMessage.setBannerImages(zxMessage.getBannerImages());
            zxMessage.setIsIndexImage(1);
            zxMessageService.updateByPrimaryKeySelective(zxMessage);  
        }
        
            
        return JSONUtils.serialize(new Ret(0,count));
    }
    
    @RequestMapping("/notBecomeBanner")
    @ResponseBody
    public String notBecomeBanner(String id){
        ZxMessageExample example = new ZxMessageExample();
        example.createCriteria().andIdEqualTo(id);
        List<ZxMessage>list = zxMessageService.selectByExample(example);
        ZxMessage zxmessage = list.get(0);
        zxmessage.setBannerImages("");
        zxmessage.setIsIndexImage(0);
        zxmessage.setOnlyShowImage(0);
        zxMessageService.updateByPrimaryKeySelective(zxmessage);
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/onlyShowImage")
    @ResponseBody
    public String onlyShowImage(String id){
        ZxMessageExample example = new ZxMessageExample();
        example.createCriteria().andIsIndexImageEqualTo(1).andIdEqualTo(id);
        List<ZxMessage> list = zxMessageService.selectByExample(example);
        ZxMessage zxMessage = list.get(0);
        zxMessage.setOnlyShowImage(1);
        zxMessageService.updateByPrimaryKeySelective(zxMessage);  
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/notOnlyShowImage")
    @ResponseBody
    public String notOnlyShowImage(String id){
        ZxMessageExample example = new ZxMessageExample();
        example.createCriteria().andIsIndexImageEqualTo(1).andIdEqualTo(id);
        List<ZxMessage> list = zxMessageService.selectByExample(example);
        ZxMessage zxMessage = list.get(0);
        zxMessage.setOnlyShowImage(0);
        zxMessageService.updateByPrimaryKeySelective(zxMessage);  
        return JSONUtils.serialize(new Ret(0));
    }
}